package com.siwei.mes.controller.laboratory;

import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.util.NullUtils;
import com.siwei.mes.entity.laboratory.ContractData;
import com.siwei.mes.entity.laboratory.CustomData;
import com.siwei.mes.entity.materials.MaterialsSpecConfig;
import com.siwei.mes.entity.sync.erp.ErpCustomer;
import com.siwei.mes.entity.sync.erp.ErpProject;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.mapper.laboratory.ContractInfoMapper;
import com.siwei.mes.mapper.laboratory.CustomInfoMapper;
import com.siwei.mes.mapper.syncErp.ErpCustomerMapper;
import com.siwei.mes.mapper.syncErp.ErpProjectMapper;
import com.siwei.mes.service.system.InitializationService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * 同步协会相关数据到品控
 *
 * @Description: 同步协会相关数据到品控
 * @Version: 1.0
 * @author: mlchen
 * @date: 2024/9/13 22:02
 */
@RestController
@RequestMapping("syncLaboratory")
public class SyncLaboratoryController {
    private static final Logger log = LoggerFactory.getLogger("laboratory");
    @Resource
    private CustomInfoMapper customInfoMapper;
    @Resource
    private ErpCustomerMapper erpCustomerMapper;
    @Resource
    private ContractInfoMapper contractInfoMapper;
    @Resource
    private ErpProjectMapper erpProjectMapper;
    @Resource
    private InitializationService initializationService;

    @AnonymousAccess
    @ApiOperation(value = "下载客户信息到品控")
    @GetMapping("/customInfo")
    public Json customInfo() {
        log.info("下载客户信息到品控开始");
        List<CustomData> customDataList = customInfoMapper.getAll();
        List<ErpCustomer> erpCustomerList = new ArrayList<>();

        for (CustomData customData : customDataList) {
            ErpCustomer erpCustomer = new ErpCustomer();
            erpCustomer.setId(null);
            erpCustomer.setLaboratoryId(customData.getCustId());
            erpCustomer.setCustomerNo(customData.getCustNo());
            erpCustomer.setCustomerName(customData.getCustUnitName());
            erpCustomer.setCustomerAddress(customData.getCustUnitAddress());
            erpCustomer.setLegalPerson(customData.getCustLegalRepresentative());
            erpCustomer.setConsigner(customData.getCustConsigner());
            erpCustomer.setLinkman(customData.getCustLinkMan());
            erpCustomer.setLinkPhone(customData.getCustLinkPhone());
            erpCustomer.setCreateTime(customData.getCustCreateDate());
            erpCustomer.setUpdateTime(customData.getCustCreateDate());
            erpCustomer.setIsDel(0);
            erpCustomer.setIsUpload(1);
            erpCustomer.setErrorMsg(null);
            erpCustomerList.add(erpCustomer);
        }
        if (!erpCustomerList.isEmpty()) {
            erpCustomerMapper.insertList(erpCustomerList);
        }
        log.info("下载客户信息到品控结束，新增条数：【{}】", erpCustomerList.size());
        return Json.succ().msg("下载协会客户信息到品控成功，新增客户数据：【" + erpCustomerList.size() + "】条");
    }

    @AnonymousAccess
    @ApiOperation(value = "下载合同信息到品控")
    @GetMapping("/contractInfo")
    public Json contractInfo() {
        log.info("下载合同信息到品控开始");
        List<ContractData> contractDataList = contractInfoMapper.getAll();

        List<ErpCustomer> list = erpCustomerMapper.getList(new ErpCustomer());
        /**erp客户map  key:协会id  value:品控id*/
        Map<String, Long> customerListMap = new HashMap<>();
        for (ErpCustomer erpCustomer : list) {
            if (StringUtils.isBlank(erpCustomer.getLaboratoryId())) {
                continue;
            }
            if (customerListMap.containsKey(erpCustomer.getLaboratoryId())) {
                continue;
            }
            customerListMap.put(erpCustomer.getLaboratoryId(), erpCustomer.getId());
        }

        List<ErpProject> erpProjectList = new ArrayList<>();

        for (ContractData contractData : contractDataList) {
            ErpProject erpProject = new ErpProject();
            erpProject.setId(null);
            erpProject.setLaboratoryId(contractData.getContractId());
            erpProject.setErpCustomerId(customerListMap.get(contractData.getCustId()));
            erpProject.setContractNo(contractData.getContractNo());
            erpProject.setContractName(null);
            erpProject.setProjectName(contractData.getContractProjectName());
            erpProject.setLaboratoryProjectName(contractData.getContractProjectName());
            erpProject.setProjectAddress(contractData.getContractProjectAddress());
            erpProject.setSignDate(NullUtils.ifNullDate(contractData.getContractSignDate()));
            erpProject.setBjbh(contractData.getContractBjbh());
            erpProject.setJdh(contractData.getContractBjbh());
            erpProject.setJsdw(contractData.getContractJsdw());
            erpProject.setSgdw(contractData.getContractSgdw());
            erpProject.setJldw(contractData.getContractJldw());
            erpProject.setContractType(NullUtils.ifNullInteger(contractData.getContractType(), 1));
            erpProject.setCreateTime(erpProject.getSignDate() == null ? new Date() : erpProject.getSignDate());
            erpProject.setUpdateTime(erpProject.getSignDate() == null ? new Date() : erpProject.getSignDate());
            erpProject.setIsDel(0);
            erpProject.setIsUpload(1);
            erpProjectList.add(erpProject);
        }
        if (!erpProjectList.isEmpty()) {
            erpProjectMapper.insertList(erpProjectList);
        }
        log.info("下载合同信息到品控结束，新增条数：【{}】", erpProjectList.size());
        return Json.succ().msg("下载合同信息到品控成功，新增工程和合同数据：【" + erpProjectList.size() + "】条");
    }


    /**
     * 同步原材料信息
     * 对应协会的item_item表中的itemId
     * 1-水泥   1101
     * 2-粉煤灰 1117
     * 3-矿渣粉 1124
     * 4-粗骨料 1102
     * 5-细骨料 1103,1203
     * 6-外加剂 1116
     * 7-混凝土 1112,1108,1110,1109
     *
     * @return
     */

    @AnonymousAccess
    @ApiOperation(value = "下载协会材料信息到品控")
    @GetMapping("/materialsInfo")
    public Json materialsInfo() {
        log.info("下载协会材料信息到品控开始");
        Map<String, Object> map = new HashMap<>();
        MaterialsEnum[] values = MaterialsEnum.values();
        for (MaterialsEnum materialsEnum : values) {
            if (StringUtils.isBlank(materialsEnum.getLaboratoryItemId())) {
                continue;
            }
            log.info("开始初始化【{}】", materialsEnum.getName());
            List<MaterialsSpecConfig> list = initializationService.initMaterials(materialsEnum.getLaboratoryItemId(), materialsEnum.getCode());
            map.put(materialsEnum.getName(), list.size());
        }
        return Json.succ().data(map);
    }
}
